package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.JavaBean.NaoZhongJavaBean;
import com.example.allplan.SQhelper.NaoZhongSQhelper;

import java.util.ArrayList;

public class NaoZhongSQCaoZuo {
    public NaoZhongSQhelper msq;
    public Context con;
    public SQLiteDatabase mdb;
    public NaoZhongSQCaoZuo(Context context){
        con=context;
        msq=new NaoZhongSQhelper(con,"suo_you_nao_zhong",null,1);
    }
    public  synchronized void openRead(){
        mdb=msq.getReadableDatabase();
    }
    public synchronized void openWrite(){
        mdb=msq.getWritableDatabase();
    }
    public synchronized void close(){
        mdb.close();
    }
    public synchronized ArrayList<NaoZhongJavaBean> readall(){
        ArrayList<NaoZhongJavaBean> mList=new ArrayList<NaoZhongJavaBean>();
        openWrite();
        Cursor cursor = mdb.query("nao_zhong_biao", new String[]{"id","kai_shi_zhi","chi_xu_zhi","jie_shu_zhi","nao_zhong_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new NaoZhongJavaBean(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("kai_shi_zhi")),cursor.getInt(cursor.getColumnIndex("chi_xu_zhi")),cursor.getInt(cursor.getColumnIndex("jie_shu_zhi")),cursor.getString(cursor.getColumnIndex("nao_zhong_ming"))));
        }
        close();
        return mList;
    }
    public synchronized void writeone(NaoZhongJavaBean o){
        openWrite();
        mdb.insert("nao_zhong_biao",null,bean_to_values(o));
        close();
    }
    public synchronized void deleteone(NaoZhongJavaBean o){
        openWrite();
        mdb.delete("nao_zhong_biao","id=?",new String[]{o.getId()+""});
        close();
    }
    private ContentValues bean_to_values(NaoZhongJavaBean o) {
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("kai_shi_zhi",o.getKai_shi_zhi());
        values.put("chi_xu_zhi",o.getChi_xu_zhi());
        values.put("jie_shu_zhi",o.getJie_shu_zhi());
        values.put("nao_zhong_ming",o.getNao_zhong_ming());
        return values;


    }
}
